package com.koron.css2.systemManage.service;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.common.utils.HttpUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoleOrgService {
	
	private static Logger log = LogManager.getLogger(RoleOrgService.class);
	
	public static final String HEADER_COM_ROLE = "1";  
	
	public static final String SUB_COM_ROLE = "2";
	
	public void addRoleOrg(String orgCode, List<String> roleCodeList) {
		if(StringUtils.isEmpty(orgCode)) {
			throw new IllegalArgumentException("orgCode 不能为空");
		}
		if(roleCodeList == null) {
			throw new IllegalArgumentException("roleCodeList 不能为空");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantCode", ApplicationConfig.getAuthorityTenantCode());
		param.put("roleCode", orgCode);
		param.put("roleCodeList", roleCodeList);
		String url = ApplicationConfig.getAuthorityUrl() + ApplicationConfig.ADD_ROLE_ORG_URL;
		String result = HttpUtils.sendPostJson(url, JsonUtils.objectToJson(param));
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("添加角色组织失败");
		}
	}
	
	public void deleteRoleOrg(String orgCode, List<String> roleCodeList) {
		if(StringUtils.isEmpty(orgCode)) {
			throw new IllegalArgumentException("orgCode 不能为空");
		}
		if(roleCodeList == null) {
			throw new IllegalArgumentException("roleCodeList 不能为空");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("tenantCode", ApplicationConfig.getAuthorityTenantCode());
		param.put("roleCode", orgCode);
		param.put("roleCodeList", roleCodeList);
		String url = ApplicationConfig.getAuthorityUrl() + ApplicationConfig.DEL_ROLE_ORG_URL;
		String result = HttpUtils.sendPostJson(url, JsonUtils.objectToJson(param));
		JSONObject json = JSONObject.fromObject(result);
		if(json.getInt("code") != 0) {
			log.error(json.getString("description"));
			throw new RuntimeException("删除角色组织失败");
		}
	}
	
}
